package com.mew.slots.core.game.szxz.service;

import com.alibaba.fastjson.JSON;
import com.mew.common.enm.SlotsWinType;
import com.mew.common.entity.game.WinFree;
import com.mew.slots.core.common.game.BaseGameService;
import com.mew.slots.core.common.game.vo.BasePlayMsgs;
import com.mew.slots.core.common.messge.response.SlotsPlayResponseMessage;
import com.mew.slots.core.common.vo.StrategyGameVO;
import com.mew.slots.core.game.szxz.dto.SzxzInRoom;
import com.mew.slots.core.game.szxz.dto.SzxzPlayMsgs;
import com.mew.slots.core.game.szxz.message.SzxzPlayResponseMessage;
import com.mew.slots.core.game.szxz.roller.SzxzXml;
import com.mew.slots.core.game.szxz.room.SzxzRoom;
import org.apache.commons.lang3.StringUtils;

/**
 * @author dylin
 */
public class SzxzService extends BaseGameService {
    @Override
    public BasePlayMsgs startGame(StrategyGameVO player) {
        WinFree winFree=player.getWinFree();
        SzxzInRoom vo;
        if (StringUtils.isNotEmpty(winFree.getJson())){
            vo = JSON.parseObject(winFree.getJson(),SzxzInRoom.class);
        }else{
            vo = new SzxzInRoom();
        }
        SzxzRoom room = new SzxzRoom(vo,winFree.getFreeType(),winFree.getFreeNumber());
        room.init(player.getRollerId(), SzxzXml.getInstance(),player.getWinType()==SlotsWinType.Free.code(),player.getInBetNumber(),player.getControlType());
        room.roundPlay();
        winFree.setJson(JSON.toJSONString(vo));
        return room.getRes();
    }

    @Override
    public SlotsPlayResponseMessage myResp(WinFree winFree, BasePlayMsgs res) {
        SzxzPlayResponseMessage message = new SzxzPlayResponseMessage();
        SzxzPlayMsgs pmres=(SzxzPlayMsgs) res;
        if(pmres.getBouns()>0){
            message.setBonus(pmres.getBouns());
            winFree.setBounsNumber(5);
        }
        if(pmres.getFree()==1){
            if(winFree.getFrees()==0) {
                winFree.setFreeType(pmres.getFreeType());
                message.setRemoveIcon(pmres.getRemoveIcon());
            }else{
                message.setFreeType(winFree.getFreeType());
            }
        }
        message.setCourse(pmres.getCourse());
        message.setFrees(pmres.getFreeNumber());
        message.setFreeType(pmres.getFreeType());
        message.setWildIndex(pmres.getWildIndex());
        return message;
    }

    @Override
    public void myAfterResp(SlotsPlayResponseMessage message,StrategyGameVO player, BasePlayMsgs res) {
        SzxzPlayResponseMessage szxzPlayResponseMessage = (SzxzPlayResponseMessage)message;
        szxzPlayResponseMessage.setFreeCount(player.getWinFree().getFreeCount());
    }
}
